﻿using System;
using VideoStore.Common.Enum;
using VideoStore.Domain.DAL;
using VideoStore.Domain.Entities;

namespace VideoStore.Business
{
    public class OrderBusinessObject
    {
        #region Fields

        private readonly IOrderDataAccess orderDao;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="OrderBusinessObject"/> class.
        /// </summary>
        public OrderBusinessObject()
        {
            orderDao = new OrderDataAccess();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrderBusinessObject"/> class.
        /// </summary>
        /// <param name="dao">The DAO.</param>
        public OrderBusinessObject(IOrderDataAccess dao)
        {
            // This constructor enables easy TDD as it takes in whatever IOrderDataAccess implementation.
            orderDao = dao;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the order by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Order GetOrderById(Guid id)
        {
            Order order;
            try
            {
                order = orderDao.GetOrderById(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return order;
        }

        /// <summary>
        /// Creates the order.
        /// </summary>
        /// <param name="newOrder">The new order.</param>
        /// <returns></returns>
        public Order CreateOrder(Order newOrder)
        {
            Order order;
            try
            {
                order = orderDao.CreateOrder(newOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return order;
        }

        /// <summary>
        /// Updates the order.
        /// </summary>
        /// <param name="modifiedOrder">The modified order.</param>
        /// <returns></returns>
        public Order UpdateOrder(Order modifiedOrder)
        {
            Order order;
            try
            {
                order = orderDao.UdpateOrder(modifiedOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return order;
        }

        /// <summary>
        /// Determines whether this instance [can submit order] the specified order.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <returns>
        /// 	<c>true</c> if this instance [can submit order] the specified order; otherwise, <c>false</c>.
        /// </returns>
        public bool CanSubmitOrder(Order order)
        {
            return order.status != (int)OrderStatus.Pending;
        }

        #endregion
    }
}
